﻿using System.Security.Cryptography.X509Certificates;

namespace calendar
{
    internal class Program
    {/// <summary>
     /// 输出2022年的日历
     /// </summary>
     /// <param name="args"></param>
        //设置每个月所占的宽度和高度
        const int monthWidth = 40;
        const int monthHeight = 10;
        const int dayWidth = 4;
        static void Main(string[] args)
        {
            //定义初始日期
            DateTime theDay = new DateTime(2022, 1, 1);
            //打印出月份和星期
            for (int month = 1; month < 13; month++)
            {
                //定义月份的初始位置
                int x = 0, y = 0;
                //按季度排列，一个季度为一行
                x = (month - 1) % 3 * monthWidth;
                y = (month - 1) / 3 * monthHeight;
                Console.SetCursorPosition(x + 20, y);
                Console.Write($"{month}月");
                //在月份的下一行输出星期数
                Console.SetCursorPosition(x, y + 1);
                Console.Write("日  一  二  三  四  五  六");
            }
            //定义日期的行和列
            int row = 0, col = 0;
            //从1月1日循环至12月31日
            do
            {
                //输出每个月1号
                if (theDay.Day == 1)
                {
                    row = (theDay.Month - 1) % 3 * monthWidth + ((int)theDay.DayOfWeek)*dayWidth;
                    col = (theDay.Month - 1) / 3 * monthHeight + 2;
                    Console.SetCursorPosition(row, col);
                    Console.Write("1");
                }
                if(theDay.Day != 1)
                {
                    //输出每周星期日
                    if(theDay.DayOfWeek == 0)
                    {
                        row = (theDay.Month - 1) % 3 * monthWidth + ((int)theDay.DayOfWeek)*dayWidth;
                        //换行
                        col++;
                        Console.SetCursorPosition(row, col);
                        Console.Write(theDay.Day);
                    }
                    //输出其他日期
                    else
                    {
                        row += dayWidth;
                        Console.SetCursorPosition(row, col);
                        Console.Write(theDay.Day);
                    }
                }
                //输出完成后日期加一
                theDay = theDay.AddDays(1);
            } while (theDay.Year == 2022);
            Console.WriteLine();
        }
    }
}